package com.epay;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class CreaterMapper {

	public static boolean createrMapper(Map<String,Map<String,String>> map){
		try{
			 String workpath = Constants.workpath;//文件存储路劲
			 String tableNameLower=Constants.fileName;
			 String tableName=tableNameLower.substring(0,1).toUpperCase()+tableNameLower.substring(1);
			 
			 String javaBeanPath=Constants.searchModel_package+"."+ tableName+"DO";//java bean路劲
//			  Map<String, String> map=new LinkedHashMap<String, String>();
			 //创建bean
			 String dir = workpath + File.separator + CreaterUtil.replaceAll(Constants.searchModel_package, ".", File.separator);
		     File dirfile = new File(dir);
		     if (!dirfile.exists()) {
		       dirfile.mkdirs();
		     }
		     File myBatisXml = new File(dir+"/" + tableName+"Mapper.xml");
			 FileWriter fw1 = new FileWriter(myBatisXml);
			 fw1.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
			 fw1.write("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\n");
			 fw1.write("<mapper namespace=\""+Constants.dao_package+"."+tableName+"Dao\" >\n");
			 fw1.write("<resultMap id=\"BaseResultMap\" type=\""+javaBeanPath+"\">\n");
			  for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				  Map.Entry e = (Map.Entry) it.next();
				 String dbName=String.valueOf(e.getKey());
				 Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
				 String name=pamentMap.get("name");
				 String dataType=pamentMap.get("dataType");
				     
				  //主键
//					 if("id".equals(name)){
//						 fw1.write("\t<id column=\"ID\" property=\"id\" jdbcType=\"INTEGER\" />\n");
//					 }else{
						 dataType= CreaterUtil.getJavaForDbType(dataType.toUpperCase());
						 fw1.write("\t<result column=\""+dbName+"\" property=\""+name+"\" jdbcType=\""+dataType+"\" />\n");
//					 }
			  }
		     fw1.write("</resultMap>\n");
		     fw1.write("\n");
		     

		    
		     //查询语句
		     fw1.write("\t<select id=\"getList\" parameterType=\""+javaBeanPath+"\" resultMap=\"BaseResultMap\">\n");
			 fw1.write("\t\tSELECT  *  FROM "+Constants.tableName.toLowerCase()+" \n");
			 fw1.write("\t\t<where> \n");
		     for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				 Map.Entry e = (Map.Entry) it.next();
				 String dbName=String.valueOf(e.getKey());
			     Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
			     String name=pamentMap.get("name");
			     String dataType=pamentMap.get("dataType");
			     dataType= CreaterUtil.getJavaForDbType(dataType.toUpperCase());
			     if(dataType.equals("BIGINT")||dataType.equals("INTEGER")||dataType.equals("INT")||dataType.equals("TINYINT")
						 ||dataType.equals("TIMESTAMP")||dataType.equals("DATETIME")||dataType.equals("DATE")){
			     	fw1.write("\t\t\t<if test=\""+name+" != null \">\n");
				 }else{
					fw1.write("\t\t\t<if test=\""+name+" != null and "+name+" != ''\">\n");
				 }
				//查询时替换成date类型
			     if(dataType.equals("TIMESTAMP")||dataType.equals("DATETIME")||dataType.equals("DATE")){
					 fw1.write("\t\t\t\t AND DATE_FORMAT("+dbName+", '%Y-%m-%d') = DATE_FORMAT(#{"+name+",jdbcType=DATE}, '%Y-%m-%d') \n");
				 }else{
					 fw1.write("\t\t\t\t AND "+dbName+"=#{"+name+",jdbcType="+dataType+"} \n");
				 }
				 fw1.write("\t\t\t</if>\n");
			  }

			fw1.write(" \t\t\t<if test=\"querySql != null and querySql != ''\">\n");
			fw1.write(" \t\t\t\t ${querySql} \n");
			fw1.write(" \t\t\t</if>\n");
			fw1.write("\t\t</where> \n");

			fw1.write(" \t\t<if test=\"orderField != null and orderField != ''\">\n");
			fw1.write(" \t\t\tORDER BY  ${orderField} ${orderType}\n");
			fw1.write("\t\t</if>\n");
			fw1.write("\t\t<if test=\"orderField == null\">\n");
			fw1.write(" \t\t\tORDER BY ID DESC\n");
			fw1.write("\t\t</if>\n");

		    fw1.write(" \t</select>\n");
		    fw1.write("\n");
		    
		    //查询语句
		     fw1.write("\t<select id=\"get\" parameterType=\""+javaBeanPath+"\" resultMap=\"BaseResultMap\">\n");
			 fw1.write("\t\tSELECT  *  FROM "+Constants.tableName.toLowerCase()+"\n");
			 fw1.write("\t\t<where> \n");
		     for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				 Map.Entry e = (Map.Entry) it.next();
				 String dbName=String.valueOf(e.getKey());
			     Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
			     String name=pamentMap.get("name");
			     String dataType=pamentMap.get("dataType");
			     dataType= CreaterUtil.getJavaForDbType(dataType.toUpperCase());
				 if(dataType.equals("BIGINT")||dataType.equals("INTEGER")||dataType.equals("INT")||dataType.equals("TINYINT")
						 ||dataType.equals("TIMESTAMP")||dataType.equals("DATETIME")||dataType.equals("DATE")){
					 fw1.write("\t\t\t<if test=\""+name+" != null \">\n");
				 }else{
					 fw1.write("\t\t\t<if test=\""+name+" != null and "+name+" != ''\">\n");
				 }
				 //查询时替换成date类型
				 if(dataType.equals("TIMESTAMP")||dataType.equals("DATETIME")||dataType.equals("DATE")){
					 fw1.write("\t\t\t\t AND DATE_FORMAT("+dbName+", '%Y-%m-%d') = DATE_FORMAT(#{"+name+",jdbcType=DATE}, '%Y-%m-%d') \n");
				 }else{
					 fw1.write("\t\t\t\t AND "+dbName+"=#{"+name+",jdbcType="+dataType+"} \n");
				 }
				 fw1.write("\t\t\t</if>\n");
			  }
			fw1.write("\t\t</where> \n");

		    fw1.write("\t\tORDER BY ID  DESC\n");
			fw1.write("\t\t limit 0,1\n");
		    fw1.write("\t</select>\n");
		   
		    fw1.write("\n");
		    
		    
		    //修改语句
		    fw1.write("\t<update id=\"update\" parameterType=\""+javaBeanPath+"\" >\n");
		    fw1.write("\t\tUPDATE "+Constants.tableName.toLowerCase()+"\n");
		    fw1.write("\t\tSET \n");
		    int i=0;
		    for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
		    	i++;
				 Map.Entry e = (Map.Entry) it.next();
				 String dbName=String.valueOf(e.getKey());
			     Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
			     String name=pamentMap.get("name");
			     String dataType=pamentMap.get("dataType");
			     System.out.println(dbName.toUpperCase()+"-----");
			     if(dbName.toUpperCase().equals("ID")){
			    	 continue;
			     }
			     dataType= CreaterUtil.getJavaForDbType(dataType.toUpperCase());
			     if(i==map.size()){
			    	 fw1.write("\t\t"+dbName+"= #{"+name+",jdbcType="+dataType+"}\n");
			     }else{
			    	 fw1.write("\t\t"+dbName+"= #{"+name+",jdbcType="+dataType+"},\n");
			     }
			  }
		    fw1.write("\t\t WHERE ID = #{id,jdbcType=INTEGER}\n");
		    fw1.write("\t</update>\n");
		    fw1.write("\n");
		    
		    //新增
		    String keys="";
		    String values="";
		    for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				 Map.Entry e = (Map.Entry) it.next();
				 String dbName=String.valueOf(e.getKey());
			     Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
			     String name=pamentMap.get("name");
			     String dataType=pamentMap.get("dataType");
			     if(dbName.toUpperCase().equals("ID")){
			    	 continue;
			     }
			    keys=keys+dbName+",";
			    dataType= CreaterUtil.getJavaForDbType(dataType.toUpperCase());
			    values=values+"#{"+name+",jdbcType="+dataType+"},";
			  }
		    keys=keys.substring(0,keys.length()-1);
		    values=values.substring(0,values.length()-1);
		    fw1.write("\t<insert id=\"insert\" useGeneratedKeys=\"true\" keyProperty=\"id\" parameterType=\""+javaBeanPath+"\" >\n");
		    fw1.write("\t\tINSERT INTO "+Constants.tableName.toLowerCase()+" ("+keys+")\n");
		      
		    fw1.write("\t\tVALUES ("+values+")\n");
		    fw1.write("\t</insert>\n");
		    fw1.write("\n");
		    
		    //删除
		    fw1.write("\t<delete id=\"delete\" parameterType=\"java.lang.Integer\" >\n");
		    fw1.write("\t\tDELETE FROM "+Constants.tableName.toLowerCase()+" \n");
			fw1.write("\t\t<where> \n");
		    for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				 Map.Entry e = (Map.Entry) it.next();
				 String dbName=String.valueOf(e.getKey());
			     Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
			     String name=pamentMap.get("name");
			     String dataType=pamentMap.get("dataType");
				dataType= CreaterUtil.getJavaForDbType(dataType.toUpperCase());
				if(dataType.equals("BIGINT")||dataType.equals("INTEGER")||dataType.equals("INT")||dataType.equals("TINYINT")
						||dataType.equals("TIMESTAMP")||dataType.equals("DATETIME")||dataType.equals("DATE")){
					fw1.write("\t\t\t<if test=\""+name+" != null \">\n");
				}else{
					fw1.write("\t\t\t<if test=\""+name+" != null and "+name+" != ''\">\n");
				}
				//查询时替换成date类型
				if(dataType.equals("TIMESTAMP")||dataType.equals("DATETIME")||dataType.equals("DATE")){
					fw1.write("\t\t\t\t AND DATE_FORMAT("+dbName+", '%Y-%m-%d') = DATE_FORMAT(#{"+name+",jdbcType=DATE}, '%Y-%m-%d') \n");
				}else{
					fw1.write("\t\t\t\t AND "+dbName+"=#{"+name+",jdbcType="+dataType+"} \n");
				}
				fw1.write("\t\t\t</if>\n");
			  }
			fw1.write(" \t\t\t<if test=\"querySql != null and querySql != ''\">\n");
			fw1.write(" \t\t\t\t ${querySql} \n");
			fw1.write(" \t\t\t</if>\n");
			fw1.write("\t\t</where> \n");
		    fw1.write("\t</delete>\n");
		    fw1.write("\n");
		    //删除
		    fw1.write("\t<delete id=\"deleteByPrimaryKey\" parameterType=\"java.lang.Integer\" >\n");
		    fw1.write("\t\tDELETE FROM "+Constants.tableName.toLowerCase()+"\n");
		    fw1.write("\t\tWHERE id = #{id,jdbcType=INTEGER}\n");
		    fw1.write("\t</delete>\n");
		    fw1.write("</mapper>\n");
			fw1.close();
		}catch(Exception e){
			System.out.println("创建mapper异常");
			e.printStackTrace();
		}
		return true;
	}
}
